<%--
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
--%>
<%@ tag import="org.jfree.chart.JFreeChart" %>
<%@ tag import="org.jfree.chart.axis.DateAxis" %>
<%@ tag import="org.jfree.chart.axis.NumberAxis" %>
<%@ tag import="org.jfree.chart.plot.XYPlot" %>
<%@ tag import="org.jfree.chart.renderer.xy.DefaultXYItemRenderer" %>
<%@ tag import="org.jfree.data.time.Day" %>
<%@ tag import="org.jfree.data.time.TimeSeries" %>
<%@ tag import="org.jfree.data.time.TimeSeriesCollection" %>
<%@ tag import="org.xptracker.services.reports.ReportFactory" %>
<%@ tag import="org.xptracker.services.reports.StoryHistory" %>
<%@ tag import="org.xptracker.web.ChartActionBean" %>
<%@ tag import="org.xptracker.web.SessionContext" %>
<%@ tag import="java.awt.*" %>
<%@ tag import="java.util.Date" %>
<%@ tag import="java.util.SortedMap" %>
<%@ include file="/taglibs.jsp" %>
<%@ attribute name="story" type="org.xptracker.domain.Story" %>
<%@ attribute name="style" type="java.lang.String" required="false" %>
<%
  StoryHistory<Long> effortHistory = ReportFactory.getInstance().makeStoryHistory(StoryHistory.EFFORT, story.getId());
  StoryHistory<Double> estimateHistory = ReportFactory.getInstance().makeStoryHistory(StoryHistory.ESTIMATES, story.getId());
  SortedMap<Date, Long> effortMap = effortHistory.getTimeline();
  SortedMap<Date, Double> estimateMap = estimateHistory.getTimeline();

  TimeSeriesCollection dataset = new TimeSeriesCollection();
  TimeSeries effortSeries = new TimeSeries("Effort");
  for (Date d : effortMap.keySet()) {
    Double value = effortMap.get(d) == null ? 0.0 : effortMap.get(d);
    effortSeries.addOrUpdate(new Day(d), value / 1000 / 60 / 60);
  }
  dataset.addSeries(effortSeries);

  TimeSeries estimateSeries = new TimeSeries("Estimate");
  for (Date d : estimateMap.keySet()) {
    Double value = estimateMap.get(d) == null ? 0.0 : estimateMap.get(d);
    estimateSeries.addOrUpdate(new Day(d), value);
  }
  dataset.addSeries(estimateSeries);

  XYPlot plot = new XYPlot(dataset, new DateAxis(), new NumberAxis(), new DefaultXYItemRenderer());
  JFreeChart chart = new JFreeChart(plot);
  chart.setBackgroundPaint(Color.WHITE);
  String uuid = ChartActionBean.storeChart(new SessionContext() {
    public void setAttribute(String key, Object value) {
      jspContext.setAttribute(key, value, PageContext.SESSION_SCOPE);
    }
  }, chart);
  jspContext.setAttribute("uuid", uuid);
  jspContext.setAttribute("varname", uuid.replaceAll("-", ""));
%>
<s:url beanclass="org.xptracker.web.ChartActionBean" var="chartUrl"/>
<span id="history-${uuid}" style="display:none">
  <img src="${chartUrl}?id=${uuid}&width=320&height=240" alt="Story History" style="${style}"/>
  <a href="javascript:hide_${varname}()">Hide</a>
</span>
<a id="display-link-${uuid}" href="javascript:show_${varname}()">Show</a>
<script type="text/javascript">
  function show_${varname}() {
    Element.show("history-${uuid}");
    Element.hide("display-link-${uuid}");
  }
  function hide_${varname}() {
    Element.hide("history-${uuid}");
    Element.show("display-link-${uuid}");
  }
</script>